package sdc.com.petscalendar.adapters;

import java.util.ArrayList;
import java.util.List;

import sdc.com.petscalendar.R;
import sdc.com.petscalendar.objects.Treament;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TreamentAdapter extends BaseAdapter {
	private ItemClickListener itemClickListener;
	private List<Treament> parasites;
	private List<Treament> filteredParasites;
	private Context context;

	public TreamentAdapter(Context context, List<Treament> objects) {
		this.context = context;
		this.parasites = objects;
		this.filteredParasites = objects;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		View v = convertView;
		if (convertView == null) {
			v = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.layout_treatment_item, parent, false);
			holder = new ViewHolder();
			holder.tvName = (TextView) v.findViewById(R.id.tv_treament_name);
			holder.ivAvatar = (ImageView) v
					.findViewById(R.id.iv_treament_avatar);

			v.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		Treament treament = getItem(position);
		holder.tvName.setText(treament.getName());

		if (treament.getProduct() != null
				&& treament.getProduct().getImage() != null
				&& treament.getProduct().getImage().length() > 0) {
			Bitmap bitmap = BitmapFactory.decodeFile(treament.getProduct()
					.getImage());
			holder.ivAvatar.setImageBitmap(bitmap);
		}

		v.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (itemClickListener != null) {
					itemClickListener.onItemClick(position);
				}
			}
		});

		return v;
	}

	@Override
	public int getCount() {
		return filteredParasites.size();
	}

	@Override
	public Treament getItem(int position) {
		if (position >= 0 && position < filteredParasites.size())
			return filteredParasites.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setItemClickListener(ItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
	}

	public void filter(int type) {
		if (type == Treament.INTERNAL || type == Treament.EXTERNAL) {
			filteredParasites = new ArrayList<Treament>();
			for (Treament p : parasites) {
				if (p.getType() == type) {
					filteredParasites.add(p);
				}
			}
		} else {
			filteredParasites = parasites;
		}
	}

	private class ViewHolder {
		private TextView tvName;
		private ImageView ivAvatar;
	}

	public interface ItemClickListener {
		public void onItemClick(int position);
	}
}
